package cn.edu.abtu.service.impl;

import cn.edu.abtu.dao.EduSubjectDao;
import cn.edu.abtu.dao.OneSubjectDao;
import cn.edu.abtu.entity.excel.SubjectData;
import cn.edu.abtu.exceptionhandler.GuliException;
import cn.edu.abtu.listener.SubjectExcelListener;
import cn.edu.abtu.service.EduSubjectService;
import cn.edu.abtu.vo.ResponseData;
import com.alibaba.excel.EasyExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;


@Service
public class EduSubjectServiceImpl implements EduSubjectService {

    @Autowired
    private EduSubjectDao eduSubjectDao;

    @Autowired
    private OneSubjectDao oneSubjectDao;

    @Override
    public ResponseData uploadSubject(MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            EasyExcel.read(inputStream, SubjectData.class,new SubjectExcelListener(eduSubjectDao)).sheet().doRead();
            return ResponseData.success(20000,"添加课程分类成功",null);
        }catch (Exception e){
            e.printStackTrace();
            throw new GuliException(20001,"数据格式异常");
        }
    }

    @Override
    public ResponseData getAllSubject() {
        List<OneSubjectDao> allSubject = oneSubjectDao.getAllSubject();
        return ResponseData.success(allSubject);
    }
}
